from django.db import models
import uuid
from control.models.User import User


class UnitPrefix(models.Model):
    uuid = models.UUIDField(primary_key=True, auto_created=True,
                            default=uuid.uuid4, editable=False, verbose_name="uuid")
    prefix_name = models.CharField(
        max_length=200, verbose_name="单位词头名称", null=False, blank=True,default="")
    prefix_name_en = models.CharField(
        max_length=200, verbose_name="单位词头英文名称", null=False, blank=True, default="")
    sei_id = models.CharField(
        max_length=200, verbose_name="单位词头编码", null=False, blank=True, default="")
    prefix_base = models.FloatField(null=False, blank=False,
                                    default=1.0, verbose_name="单位词头底数")
    prefix_power = models.FloatField(null=False, blank=False,
                                     default=1.0, verbose_name="单位词头指数")
    prefix_symbol = models.CharField(
        max_length=256, null=False, blank=False, default="", verbose_name="单位词头符号")
    prefix_symbol_ascii = models.CharField(
        max_length=256, null=False, blank=True, default="", verbose_name="单位词头ascii符号")
    prefix_symbol_html = models.CharField(
        max_length=256, null=False, blank=True, default="", verbose_name="单位词头html符号")
    prefix_symbol_latex = models.CharField(
        max_length=256, null=False, blank=True, default="", verbose_name="单位词头latex符号")
    prefix_symbol_unicode = models.CharField(
        max_length=256, null=False, blank=True, default="", verbose_name="单位词头unicode符号")
    prefix_description = models.TextField(
        verbose_name='单位词头描述', null=False, blank=True, default="")
    prefix_description_en = models.TextField(
        verbose_name='单位词头英文描述', null=False, blank=True, default="")
    prefix_comment = models.TextField(
        verbose_name='单位词头备注', null=False, blank=True, default="")
    create_person = models.ForeignKey(
        User, verbose_name="创建人", related_name="create_unitprefix", on_delete=models.DO_NOTHING, null=False)
    create_date = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    modify_person = models.ForeignKey(
        User, verbose_name="修改人", related_name="modify_unitprefix", on_delete=models.DO_NOTHING, null=False)
    modify_date = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    valid_flag = models.BooleanField(default=True, verbose_name="是否有效")

    class Meta:
        ordering = ['create_date']
        verbose_name = "UnitPrefix"
        verbose_name_plural = "UnitPrefixs"
        constraints = [
            models.UniqueConstraint(fields=["prefix_name"], condition=models.Q(
                valid_flag=True) & ~models.Q(prefix_name=""), name="unique_prefix_name"),
            models.UniqueConstraint(fields=["prefix_name_en"], condition=models.Q(
                valid_flag=True) & ~models.Q(prefix_name_en=""), name="unique_prefix_name_en"),
            models.UniqueConstraint(fields=["sei_id"], condition=models.Q(
                valid_flag=True) & ~models.Q(sei_id=""), name="unique_valid_prefix_sei_id"),
        ]

    def __str__(self):
        return "{}({})".format(self.prefix_name, self.prefix_name_en)
            
    def show_full_name(self):
        if self.prefix_name != "" and self.prefix_name_en != "":
            return "{}({})".format(self.prefix_name, self.prefix_name_en)
        if self.prefix_name != "":
            return self.prefix_name
        if self.prefix_name_en != "":
            return self.prefix_name_en
        return "未命名单位词头"
    
    def show_short_name(self):
        if self.prefix_name != "":
            return self.prefix_name
        if self.prefix_name_en != "":
            return self.prefix_name_en
        return "未命名单位词头"